﻿dojo.require("esri.map");
dojo.require("esri.tasks.query");

var isClickMap = false; //标志是否在地图上点击
var _R_ID="000", _P_ID="000", _C_ID="000", _D_ID="000", _S_ID="000"; //各级行政区的ID

var target_level = -1;  //右边区域中点击时地图要缩放的目标级别

var onlyShowData = false; //标志是否仅显示柱形数据

var SERVICE_URL = "http://user-think/ArcGIS/rest/services/%E5%95%86%E5%9C%88%E8%B5%A2%E5%AE%B6-2011-1-13/MapServer";
var LEVEL_COUNT = 6;

var map, queryTask, query, layer;
var symbol, infoTemplate;
var lods = [
    {
        "level": 0,
        "resolution": 0.0000951784402,
        "scale": 40000
    },
    {
        "level": 1,
        "resolution": 0.0005948652515,
        "scale": 250000
    },
    {
        "level": 2,
        "resolution": 0.0009517844023,
        "scale": 400000
    },
    {
        "level": 3,
        "resolution": 0.0142767660350,
        "scale": 6000000
    },
    {
        "level": 4,
        "resolution": 0.0356919150875,
        "scale": 15000000
    },
    {
      "level": 5,
      "resolution": 0.0951784402332,
      "scale": 40000000
    }
];

//init map
function init() 
{
    map = new esri.Map("mapDiv", 
    {
        extent: new esri.geometry.Extent(
        {
            xmin: 64.2147224728262,
            ymin: -4.25535957978761,
            xmax: 144.358392207174,
            ymax: 75.88831015456,
            spatialReference: {
                wkid: 4326
            }
        }),
        lods: lods,
        logo: false
    });
    
    //init layer
    layer = new esri.layers.ArcGISDynamicMapServiceLayer(SERVICE_URL);
    map.addLayer(layer);
    
    //init query & set out field
    query = new esri.tasks.Query();
    query.returnGeometry = true;
    query.outSpatialReference = {wkid:4326};
    query.outFields = ["NAME","MID","R_ID"];
    
    //listen map event
    dojo.connect(map, "onExtentChange", showExtent);
    dojo.connect(map, "onClick", executeQueryTask);
}

//set special layer to background
function setDefaultVisibleLayers(lays)
{
    var visible = lays;
    if(!visible)
    {
        visible = [0,3,6,9,13,14];
    }
    layer.setVisibleLayers(visible);
}

//event function for map.onExtentChange 
function showExtent(extent, delta, outLevelChange, outLod) 
{
    setDefaultVisibleLayers();
    layerid = -1;
    //alert("showExtent:" + map.getLevel());
    
    //mapped outLod.level to layerid
    switch (map.getLevel())
    {
       case 0:
          layerid = 14;
          break;
       case 1:
          layerid = 13;
          break;
       case 2:
          layerid = 9;
          break;
       case 3:
          layerid = 6;
          break;
       case 4:
          layerid = 3;
          break;
       case 5:
          layerid = 0;
          break;
    }
    //after map initialized init queryTask
    queryTask = new esri.tasks.QueryTask(SERVICE_URL+"/"+layerid);
}

//ecent function for map.onClick
function executeQueryTask(evt) 
{
    isClickMap = true;
    query.geometry = evt.mapPoint;
    queryTask.execute(query, showResults);
    map.centerAndZoom(evt.mapPoint, map.getLevel()+1);
}

//call back function for query.execute 
function showResults(featureSet) 
{
    map.graphics.clear();
    var features = featureSet.features;
    
    if(isClickMap)
    {
        var feature, attributes;
        il = featureSet.features.length;
        //alert(il);
        //iterate features & read out field to variable
        for (var i = 0; i < il; i++) 
        {
            feature = features[i];
            attributes = feature.attributes;
            //alert(attributes.NAME + "," + attributes.MID);
            
            if(map.getLevel() == 0 || map.getLevel() == 1)
            {
                _R_ID = "00" + attributes.R_ID;
            }
        
            if(attributes.MID) splitMIDToSpecialID(attributes.MID);
            rightFrameGotoURL(map.getLevel());
            isClickMap = false;
        }
    }
}

//split MID to special ID
function splitMIDToSpecialID(_MID)
{
    _P_ID = _MID.substring(0, 3);
    _C_ID = _MID.substring(3, 6);
    _D_ID = _MID.substring(6, 9);
    _S_ID = _MID.substring(9, 12);
}

//change right frame to special URL
function rightFrameGotoURL(_level)
{
    _url = "";
    params = "R_ID=" + _R_ID + "&P_ID=" + _P_ID + "&C_ID=" + _C_ID + "&D_ID=" + _D_ID + "&S_ID=" + _S_ID;
    
    if(_S_ID && _S_ID != "000")
    {
        _url = "modules/street/StreetView.aspx";
    }
    else if(_D_ID && _D_ID != "000")
    {
        _url = "modules/district/DistrictView.aspx";
    }
    else if(_C_ID && _C_ID != "000")
    {
        _url = "modules/city/CityView.aspx";
    }
    else if(_P_ID && _P_ID != "000")
    {
        _url = "modules/province/ProvinceView.aspx";
    }
    else if(_R_ID && _R_ID != "000")
    {
        _url = "modules/region/RegionView.aspx";
    }
    else
    {
        _url = "Quanguo.aspx";
    }
    window.parent.RightFrame.location.href = _url + "?" + params;
}

//chang map level to 0
function showQuanguo()
{
    var cntPoint = new esri.geometry.Point(104.22145, 32.08053, new esri.SpatialReference({ wkid: 4326 }));
    moveToPoint(cntPoint, 0);
}

//event function for right frame link.click
function moveToCenterByLevel(tar_level, MID, rid, pid, cid, did, sid)
{
    target_level = tar_level;
    onlyShowData = false;
    isClickMap = false;
    acbCenter.PerformCallback(callbackRequestParams(tar_level, rid, pid, cid, did, sid));
}

//move map to screen center by cntPoint and to target map level
function moveToPoint(cntPoint, tar_level)
{
	  query.geometry = cntPoint;
	  queryTask.execute(query, showResults);
	  map.centerAndZoom(cntPoint, tar_level);
}

//organize parameter to JSON
function callbackRequestParams(level, rid, pid, cid, did, sid)
{
    var pm = "{";
    pm += "Level:" + level + ",";
    pm += "RID:" + "\"" + rid + "\",";
    pm += "PID:" + "\"" + pid + "\",";
    pm += "CID:" + "\"" + cid + "\",";
    pm += "DID:" + "\"" + did + "\",";
    pm += "SID:" + "\"" + sid + "\"";
    pm += "}";
    return pm;
}

//event function for radio.click to show population data
function showInfomation(tar_level, MID, rid, pid, cid, did, sid)
{
    onlyShowData = true;
    qs = "'"+pid + "" + cid + "" + did + "" + sid + "%'";
    //alert(qs);

    var vlayers = [0,1,3,4,6,7,9,13,14];
    setDefaultVisibleLayers(vlayers);
  
    var layerDefinitions = [];
    layerDefinitions[1] = "MID like " + qs;
    layer.setLayerDefinitions(layerDefinitions);
    acbCenter.PerformCallback(callbackRequestParams(tar_level, rid, pid, cid, did, sid));
}

//event function for radio.click to show family data
function showInfomation2(tar_level, MID, rid, pid, cid, did, sid)
{
    onlyShowData = true;
    qs = "'"+pid + "" + cid + "" + did + "" + sid + "%'";
    //alert(qs);

    var vlayers = [0,2,3,5,6,8,9,13,14];
    setDefaultVisibleLayers(vlayers);
  
    var layerDefinitions = [];
    layerDefinitions[2] = "MID like " + qs;
    layer.setLayerDefinitions(layerDefinitions);
    acbCenter.PerformCallback(callbackRequestParams(tar_level, rid, pid, cid, did, sid));
}

function findImageURL()
{
    return encodeURIComponent(document.getElementById("mapDiv").getElementsByTagName("IMG")[0].src);
}

dojo.addOnLoad(init);